namespace PickGold.Charting.Utilities
{
    using System;
    using System.Globalization;
    using PickGold.Charting;

    internal static class ValueConverter
    {
        public static string FormatValue(Chart chart, object obj, object objTag, double value, string format, ChartValueType valueType, ChartElementType elementType)
        {
            format = format ?? string.Empty;
            string str = format;
            string localizedValue = "";
            if ((str != null) && (str.Length > 0))
            {
                int index = str.IndexOf('{', 0);
                if (index < 0)
                {
                    str = "{0:" + str + "}";
                }
                else
                {
                    while (index >= 0)
                    {
                        if (!str.Substring(index).StartsWith("{0:", StringComparison.Ordinal))
                        {
                            if ((index >= 1) && (str.Substring(index - 1, 1) == "{"))
                            {
                                continue;
                            }
                            str = str.Insert(index + 1, "0:");
                        }
                        index = str.IndexOf('{', index + 1);
                    }
                }
            }
            if (((valueType == ChartValueType.DateTime) || (valueType == ChartValueType.DateTimeOffset)) || (valueType == ChartValueType.Date))
            {
                if (str.Length == 0)
                {
                    str = "{0:d}";
                    if (valueType == ChartValueType.DateTimeOffset)
                    {
                        str = str + " +0";
                    }
                }
                localizedValue = string.Format(CultureInfo.CurrentCulture, str, new object[] { DateTime.FromOADate(value) });
            }
            else if (valueType == ChartValueType.Time)
            {
                if (str.Length == 0)
                {
                    str = "{0:t}";
                }
                localizedValue = string.Format(CultureInfo.CurrentCulture, str, new object[] { DateTime.FromOADate(value) });
            }
            else
            {
                bool flag = false;
                if (str.Length == 0)
                {
                    str = "{0:G}";
                }
                try
                {
                    localizedValue = string.Format(CultureInfo.CurrentCulture, str, new object[] { value });
                }
                catch (FormatException)
                {
                    flag = true;
                }
                if (flag)
                {
                    flag = false;
                    try
                    {
                        localizedValue = string.Format(CultureInfo.CurrentCulture, str, new object[] { (long) value });
                    }
                    catch (ArgumentNullException)
                    {
                        flag = true;
                    }
                    catch (FormatException)
                    {
                        flag = true;
                    }
                }
                if (flag)
                {
                    localizedValue = format;
                }
            }
            if (chart != null)
            {
                FormatNumberEventArgs e = new FormatNumberEventArgs(value, format, valueType, localizedValue, objTag, elementType);
                chart.CallOnFormatNumber(obj, e);
                localizedValue = e.LocalizedValue;
            }
            return localizedValue;
        }
    }
}

